package com.hulk.dryad.manage.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hulk.dryad.common.constant.CacheConstants;
import com.hulk.dryad.common.constant.CommonConstants;
import com.hulk.dryad.persistence.entity.SysTenantModel;
import com.hulk.dryad.persistence.mapper.SysTenantMapper;
import com.hulk.dryad.manage.provider.service.SysTenantService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 租户
 *
 * @author kim
 * @date 2019-05-15 15:55:41
 */
@Service
@AllArgsConstructor
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenantModel> implements SysTenantService {

	/**
	 * 获取正常状态租户
	 * <p>
	 * 1. 状态正常 2. 开始时间小于等于当前时间 3. 结束时间大于等于当前时间
	 * 2. 缓存时间1小时
	 * @return
	 */
	@Override
	@Cacheable(value = CacheConstants.TENANT_DETAILS+"#20s")
	public List<SysTenantModel> getNormalTenant() {
		return baseMapper
				.selectList(Wrappers.<SysTenantModel>lambdaQuery().eq(SysTenantModel::getStatus, CommonConstants.STATUS_NORMAL));
	}

	/**
	 * 保存租户
	 * <p>
	 * 1. 保存租户 2. 初始化权限相关表 - sys_user - sys_role - t_sys_menu - sys_user_role -
	 * sys_role_menu - sys_dict - sys_dict_item - sys_client_details - sys_public_params
	 * @param sysTenant 租户实体
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = CacheConstants.TENANT_DETAILS)
	public Boolean saveTenant(SysTenantModel sysTenant) {
		this.save(sysTenant);
		return Boolean.TRUE;

	}

}
